package com.jerkin.dubbo.context;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

/**
 * @version V1.0
 * @ClassName: BeanHelper
 * @Description:
 * @author: huhongzhi
 * @date: 2020/8/3 22:16
 */
@Configuration
public class BeanHelper implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        applicationContext = context;
    }

    public static <T> T getBean(Class<T> clazz) {
        try {
            return applicationContext.getBean(clazz);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static <T> T getBean(String key) {
        try {
            return (T) applicationContext.getBean(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
